package com.ww.word.ui;

import android.app.ProgressDialog;
import android.app.TabActivity;
import android.os.Bundle;
import android.os.Handler;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
import android.widget.TextView;

import com.ww.word.R;
import com.ww.word.db.WordDbHelper;
import com.ww.word.entity.Word;

/**
 * @since 
 * @version 2012-11-15
 * @author wangw
 */
public class DetailActivity extends TabActivity implements OnClickListener {
	private TabHost tabHost;
	private RadioGroup radioGroup;
	private RadioButton radio_desc, radio_play, radio_discuss;
	private TextView txt1, txt2, txt3;
	private Word word;
	private ProgressDialog mProgressDialog;//发送请求 等待的dialog

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_detail);
		tabHost = getTabHost();
		findView();
		getOpenClass();
	}

	private void findView() {
		radio_desc = (RadioButton) findViewById(R.id.radio_desc);// 取得实例
		radio_play = (RadioButton) findViewById(R.id.radio_play);// 取得实例
		radio_discuss = (RadioButton) findViewById(R.id.radio_discuss);// 取得实例
		radioGroup = (RadioGroup) findViewById(R.id.radiogroup);
		radioGroup.setOnCheckedChangeListener(checkedChangeListener);
		TextView title = (TextView) findViewById(R.id.title);// 取得实例
		title.setText(R.string.detail_title);

		findViewById(R.id.back).setOnClickListener(this);
		txt1 = (TextView) findViewById(R.id.txt1);// 取得实例
		txt2 = (TextView) findViewById(R.id.txt2);// 取得实例
		txt3 = (TextView) findViewById(R.id.txt3);// 取得实例

	}

	/**
	 * 获取课程详情.
	 * @param id 课程id
	 */
	public void getOpenClass() {
		showProgressDialog();
		new Thread(new Runnable() {
			@Override
			public void run() {
				WordDbHelper w = new WordDbHelper(DetailActivity.this);
				word = w.getWord(getIntent().getIntExtra("idWord", -1));
				handler.sendEmptyMessage(1);
			}
		}).start();
	}

	// 定义一个Handler，用来异步处理数据
	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			txt1.setText(Html.fromHtml(word.getSimple_explain()));
			txt2.setText(Html.fromHtml(word.getExplain()));
			txt3.setText(Html.fromHtml(word.getGroup()));

			tabHost.addTab(tabHost.newTabSpec("radio_desc").setIndicator("radio_desc").setContent(R.id.view1));
			tabHost.addTab(tabHost.newTabSpec("radio_play").setIndicator("radio_play").setContent(R.id.view2));
			tabHost.addTab(tabHost.newTabSpec("radio_discuss").setIndicator("radio_discuss").setContent(R.id.view3));
			hideProgressDialog();
		};
	};

	private OnCheckedChangeListener checkedChangeListener = new OnCheckedChangeListener() {
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			switch (checkedId) {
			case R.id.radio_desc:
				tabHost.setCurrentTabByTag("radio_desc");
				radio_desc.setTextColor(getResources().getColor(R.color.animtab_selected));
				radio_play.setTextColor(getResources().getColor(R.color.animtab_noselected));
				radio_discuss.setTextColor(getResources().getColor(R.color.animtab_noselected));
				break;
			case R.id.radio_play:
				tabHost.setCurrentTabByTag("radio_play");
				radio_desc.setTextColor(getResources().getColor(R.color.animtab_noselected));
				radio_play.setTextColor(getResources().getColor(R.color.animtab_selected));
				radio_discuss.setTextColor(getResources().getColor(R.color.animtab_noselected));
				break;
			case R.id.radio_discuss:
				tabHost.setCurrentTabByTag("radio_discuss");
				radio_desc.setTextColor(getResources().getColor(R.color.animtab_noselected));
				radio_play.setTextColor(getResources().getColor(R.color.animtab_noselected));
				radio_discuss.setTextColor(getResources().getColor(R.color.animtab_selected));
				break;
			default:
				break;
			}
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.back:
			finish();
			break;
		default:
			break;
		}
	}

	/**
	 * 创建进度框.
	 * @param mContext Context
	 */
	public void showProgressDialog() {
		if (mProgressDialog == null) {
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setIndeterminate(true);
			dialog.setCancelable(true);
			mProgressDialog = dialog;
		}
		try {
			mProgressDialog.show();
			mProgressDialog.setContentView(R.layout.core_loading);
		} catch (Exception e) {
		}
	}

	public void hideProgressDialog() {
		if (mProgressDialog != null) {
			mProgressDialog.dismiss();
		}
	}

}
